package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Course;
import com.yqg.education.entity.SendAnnounce;
import com.yqg.education.mapper.SendAnnounceMapper;
import com.yqg.education.service.ISendAnnounceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * SendAnnounceController 前端控制器
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/send-announce")
public class SendAnnounceController {

    @Resource
    private ISendAnnounceService sendAnnounceService;
@Resource
private SendAnnounceMapper sendAnnounceMapper;
    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody SendAnnounce sendAnnounce) {
        sendAnnounce.setCreateTime(LocalDateTime.now());
        return Result.success(sendAnnounceService.saveOrUpdate(sendAnnounce));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        SendAnnounce sendAnnounce =sendAnnounceService.getById(id);
        sendAnnounce.setIsDelete(1);
        return Result.success(sendAnnounceService.saveOrUpdate(sendAnnounce));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<SendAnnounce> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<SendAnnounce> list=  sendAnnounceService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(sendAnnounceService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
    String title) {
        pageNum = pageNum * pageSize - pageSize;
        List<SendAnnounce> courseList = sendAnnounceMapper.page(pageNum, pageSize, title);
        Long count = sendAnnounceMapper.findCount(title);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", courseList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端获取通知内容接口
     */
    @GetMapping("/stuFindSendAnnounceContent")
    public Result stuFindSendAnnounceContent(@RequestParam Integer announceId,
                                                @RequestParam Integer userId) {
        return Result.success(sendAnnounceService.stuFindSendAnnounceContent(announceId,userId));
    }
}

